package com.dongao.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.dongao.model.Course;
import com.dongao.model.CourseWare;

public class CourseDetailActivity extends Activity {
	private ViewPager mPager;// 页卡内容
	private List<View> listViews; // Tab页面列表
	private ImageView cursor;// 动画图片
	private TextView t1, t2, courseName, courseInfo;// 页卡头标
	private int offset = 0;// 动画图片偏移量
	private int currIndex = 0;// 当前页卡编号
	private int bmpW;// 动画图片宽度
	private Course course;
	private List<CourseWare> courseWares;
	
	LayoutInflater mInflater;
	
	//该应用的主布局LinearLayout
	private ViewGroup mainViewGroup;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		setContentView(R.layout.activity_course_detail);
		
		mInflater = getLayoutInflater();
		mainViewGroup = (ViewGroup)mInflater.inflate(R.layout.activity_course_detail, null);
		
		Intent intent = getIntent();
		course = (Course)intent.getSerializableExtra("course");
		courseWares = course.getCws();
		
		InitTextView();
		InitImageView();
		InitViewPager();
		
		setContentView(mainViewGroup);
	}

	/**
	 * 初始化头标
	 */
	private void InitTextView() {
		courseName = (TextView) mainViewGroup.findViewById(R.id.courseName);
		courseInfo = (TextView) mainViewGroup.findViewById(R.id.courseInfo);
		
		courseName.setText(course.getClassName());
		courseInfo.setText(course.getCwProgress() + " " + course.getUpdateTime());
		
		t1 = (TextView) mainViewGroup.findViewById(R.id.play);
		t2 = (TextView) mainViewGroup.findViewById(R.id.download);

		t1.setOnClickListener(new MyOnClickListener(0));
		t2.setOnClickListener(new MyOnClickListener(1));
	}

	/**
	 * 初始化动画
	 */
	private void InitImageView() {
		cursor = (ImageView) mainViewGroup.findViewById(R.id.cursor);
		bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.tit_bg)
				.getWidth();// 获取图片宽度
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;// 获取分辨率宽度
		offset = (screenW / 5 - bmpW) / 2;// 计算偏移量
		Matrix matrix = new Matrix();
		matrix.postTranslate(offset, 0);
		cursor.setImageMatrix(matrix);// 设置动画初始位置
		
	}

	/**
	 * 头标点击监听
	 */
	public class MyOnClickListener implements View.OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			mPager.setCurrentItem(index);
		}
	};

	/**
	 * 初始化ViewPager
	 */
	private void InitViewPager() {
		mPager = (ViewPager) mainViewGroup.findViewById(R.id.vPager);
		listViews = new ArrayList<View>();
		LayoutInflater mInflater = getLayoutInflater();
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		if (courseWares!=null) {
			for (CourseWare cw : courseWares) {
				Map<String,String> map = new HashMap<String, String>();
				map.put("name", cw.getCwName());
				System.out.println("cw:"+cw.getCwName());
				list.add(map);
			}
		}
		SimpleAdapter listAdapter1 = new SimpleAdapter(this,list,R.layout.simple_list_item,new String[]{"name"},new int[]{R.id.name});
		
		View page1 = mInflater.inflate(R.layout.simple_list, null);
		View page2 = mInflater.inflate(R.layout.simple_list, null);
		
		
		ListView list1 = (ListView)page1.findViewById(R.id.myListView);
		ListView list2 = (ListView)page2.findViewById(R.id.myListView);
		
		list1.setOnItemClickListener(new playOnline());
		
		list1.setAdapter(listAdapter1);
		list2.setAdapter(listAdapter1);
		
		listViews.add(page1);
		listViews.add(page2);
		mPager.setAdapter(new MyPagerAdapter(listViews));
		mPager.setCurrentItem(0);
		mPager.setOnPageChangeListener(new MyOnPageChangeListener());
	}
	
	private class playOnline implements OnItemClickListener{
		@Override
		public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
			CourseWare cw = courseWares.get(position);
			cw.setCwVideoUrl("http://videodl.dongao.com/2013jxjy/ydb/wsj/xjkzl/wk/13cedu_wsjxjkzl_001_1_yd/upload/media/wsj_jxjy_xjkzl_01_580x435_180kbps.m3u8");
			Intent intent = new Intent();
			intent.putExtra("mp3Info", cw);
			intent.setClass(CourseDetailActivity.this, VideoActivity.class);
			startActivity(intent);
		}
		
	}

	/**
	 * ViewPager适配器
	 */
	public class MyPagerAdapter extends PagerAdapter {
		public List<View> mListViews;

		public MyPagerAdapter(List<View> mListViews) {
			this.mListViews = mListViews;
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(mListViews.get(arg1));
		}

		@Override
		public void finishUpdate(View arg0) {

		}

		@Override
		public int getCount() {
			return mListViews.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(mListViews.get(arg1), 0);
			return mListViews.get(arg1);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == (arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}
	}
	
	/**
     * 页卡切换监听
*/
    public class MyOnPageChangeListener implements OnPageChangeListener {

        int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
        int two = one * 2;// 页卡1 -> 页卡3 偏移量

        @Override
        public void onPageSelected(int arg0) {
            /*Animation animation = null;
            switch (arg0) {
            case 0:
                if (currIndex == 1) {
                    animation = new TranslateAnimation(one, 0, 0, 0);
                } else if (currIndex == 2) {
                    animation = new TranslateAnimation(two, 0, 0, 0);
                }
                break;
            case 1:
                if (currIndex == 0) {
                    animation = new TranslateAnimation(offset, one, 0, 0);
                } else if (currIndex == 2) {
                    animation = new TranslateAnimation(two, one, 0, 0);
                }
                break;
            }
            currIndex = arg0;
            animation.setFillAfter(true);// True:图片停在动画结束位置
            animation.setDuration(300);
            cursor.startAnimation(animation);*/
        	 Animation animation = new TranslateAnimation(one*currIndex, one*arg0, 0, 0);//显然这个比较简洁，只有一行代码。  
             currIndex = arg0;  
             animation.setFillAfter(true);// True:图片停在动画结束位置  
             animation.setDuration(300);  
             cursor.startAnimation(animation); 
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }
	
}
